﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace pxt
{

    public class Wizard : BaseCharacter
    {
        // Members
        #region *MEMBERS*
        protected Charge mSuperCharge, mExtraCharge;
        protected bool mChargingSuper, mChargingExtra;
        #endregion

        // Functions
        #region *FUNCTIONS*
        public Wizard(Player own)
        {
            mPosition = new Vector2(0, 0);
            mBoundingRect = new Rectangle(0, 0, 0, 0);
            mIsAlive = true;
            mOwner = own;
            mName = "Triscuit";
            mCurHp = mMaxHp = 100;
            mSpeed = 6.0f;
            mHasSpecial = true;
            mHasReflectUp = false;
            mSuperCharge = new Charge(5.0, this);
            mExtraCharge = new Charge(2.0, this);
        }

        public override void Update(GameTime gt)
        {
            if (!mIsAlive)
                return;

            if (mChargingSuper && mHasSpecial)
            {
                mSuperCharge.Update(gt);
                if (mSuperCharge.IsFull)
                {
                    // TODO: Cast the super
                    Shoot(projectile_type.Death, new Vector2(5, 0));
                    mHasSpecial = false;
                    mSuperCharge.Reset();
                    mChargingSuper = false;
                }
                mChargingSuper = false;
            }
            else if (mChargingExtra)
            {
                mExtraCharge.Update(gt);
                if (mExtraCharge.IsFull)
                {
                    // TODO: Cast the super
                    Shoot(projectile_type.Cooldown, new Vector2(5, 0));
                    mExtraCharge.Reset();
                }
                mChargingExtra = false;
            }

            // update our positions
            mBoundingRect.X = (int)mPosition.X;
            mBoundingRect.Y = (int)mPosition.Y;


            // Check life for death
        }

        public void Load()
        {
            //this.mTexture = cm.Load<Texture2D>("sprites\\player_image");
            mTexture = GameGlobals.Content.Load<Texture2D>("sprites\\unit_frames\\player_image");
            mUnitFrameImage = GameGlobals.Content.Load<Texture2D>("sprites\\unit_frames\\player_image"); // ?? 2 of the same textures??!?
            mSpamSpellTex = GameGlobals.Content.Load<Texture2D>("sprites\\bullet");
            mCooldownTex = GameGlobals.Content.Load<Texture2D>("sprites\\cooldown");
            mDeathSpellTex = GameGlobals.Content.Load<Texture2D>("sprites\\death");
            mDeadTexture = GameGlobals.Content.Load<Texture2D>("sprites\\unit_frames\\player_dead");

            // setup the bb
            mBoundingRect.Width = mTexture.Width;
            mBoundingRect.Height = mTexture.Height;
        }

        public void Draw()
        {
            if (mChargingSuper)
            {
                mSuperCharge.Draw();
            }
            else if (mChargingExtra)
            {
                mExtraCharge.Draw();
            }
            if (mIsAlive)
            {
                GameSystems.Graphics.DrawImage(mTexture, mPosition, Color.White);
            }
            else
            {
                GameSystems.Graphics.DrawImage(mDeadTexture, mPosition, Color.White);
            }
        }

        public void Shoot(projectile_type projType, Vector2 direction)
        {
            // Shouldnt get here, but just in case!
            if (!mIsAlive)
                return;

            // shoot a projectile!

            // create a projectile
            Projectile tmp = new Projectile(projType, mPosition, direction, mOwner);

            // now set its texture based on the type
            switch (projType)
            {
                case projectile_type.Spam:
                    {
                        tmp.Texture = mSpamSpellTex;
                        break;
                    }
                case projectile_type.Cooldown:
                    {
                        tmp.Texture = mCooldownTex;
                        break;
                    }
                case projectile_type.Death:
                    {
                        tmp.Texture = mDeathSpellTex;
                        break;
                    }
            };
            if (tmp.Texture != null)
            {
                GameSystems.CollisionSystem.AddProjectile(tmp);
            }

            if (projType == projectile_type.Revive)
            {
                GameSystems.CollisionSystem.AddProjectile(tmp);
            }
        }

        public void ChargeSuper()
        {
            if (mHasSpecial)
            {
                mChargingSuper = true;
            }
        }

        public void ChargeExtra()
        {
            mChargingExtra = true;
        }

        public void ResetSuperCharge()
        {
            mSuperCharge.Reset();
        }

        public void ResetExtraCharge()
        {
            mExtraCharge.Reset();
        }
        #endregion

        // Properties
        #region *PROPERTIES*
        public float CurHealth
        {
            get { return mCurHp; }
        }

        public float MaxHealth
        {
            get { return mMaxHp; }
        }

        public float HealthPercent
        {
            get { return mCurHp / mMaxHp; }
        }

        public string Name
        {
            get { return mName; }
        }

        public Texture2D UnitFrameImage
        {
            get { return mUnitFrameImage; }
        }

        public bool Reflect
        {
            get { return mHasReflectUp; }
            set { mHasReflectUp = value; }
        }
        #endregion
        public Player Owner
        {
            get { return mOwner; }
        }
    }
}
